<?php

function renderForm($error,$action, $fname, $lname, $companyList, $company, $contactNo)
{
?>
<!----------HTML CODES----------------------------------------------------------------------------------->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta name="Description" content="Clinic Management System" />
	<link rel="stylesheet" href="../includes/Style.css" type="text/css" />
	<title>OPTICSoft</title>
</head>
<body>
<?php include('../includes/header.php');?>
<div id="content-wrap"><div id="content">

<?php displayMessage(); ?>
<?php include('sidebar.php');?>

<div id="main">	<div class="post">	
	<h1>Client Info</h1>				
	
	 <form name="main" action="" class="searchform" method="POST">
  	 <table>
  	  <tr>
  		<td align="right">First Name</td>
  		<td align="left"><input type="text" name="first" value="<?php echo $fname; ?> " /></td>
  	  </tr>
  	  <tr>
  		<td align="right">Last Name</td>
  		<td align="left"><input type="text" name="last" value="<?php echo $lname; ?> "/></td>
  	  </tr>
  	  <tr>
  		<td align="right">Company</td>
  		<td align="left">
  		  <select id="company" name="company" style="width: 115px">
  		  <?php 
  		  
  		  if($companyList==null)
		    echo '<option value="-1" selected="true"> None';
		  else
		  {
		   echo '<option value="'.$row["ID"].'"> None';
		    while ($row = mysql_fetch_assoc($companyList)) {
		    	if ($company == $row["ID"])
		          echo '<option value="'.$row["ID"].'" selected="true">'.$row["Company"];
		        else
		          echo '<option value="'.$row["ID"].'">'.$row["Company"];
		    } 
		   
		  }
		    
		  
		?>
  		</select>
  	  </tr>
  	  <tr>
  		<td align="right">Contact Num</td>
  		<td align="left"><input type="text" name="contactNo" value="<?php echo $contactNo; ?> "/></td>
  	  </tr>
  	  <tr> 
  	  	<td align="right"> 		
  		  <input type="hidden" name="message" />
	 	  <input name="submit" class="button" value="<?php echo $action; ?> " type="submit" />
	 	</td>
  	  </tr>
  	 </table> 
  	 </form>
  	 
</div></div></div></div>
</body>
</html>
<!----------------------------------------------------------------------------------------------------------->
<?php
}
  //include statement
  require("client.inc");
  $client = new Client();
  global $error;
  $companyList = $client->retrieveCompanies();
  
  //Check if the form hasn't been submitted
  if (!isset($_GET['ID']) && !isset($_POST['submit'])  )  //NO RECORD SELECTED, NEW CLIENT
  {
          renderForm('','Add', '', '', $companyList, '', '');  
  }
  else if (isset($_GET['ID']) && !isset($_POST['submit'])  )   //RECORD SELECTED FOR UPDATE
  {
         $client->retrieveByID($_GET['ID']);
  	 renderForm('','Update', $client->fname, $client->lname, $companyList, $client->company, $client->contactNo);
  }
  //Check if the form has been submitted for ADD
  else if (isset($_POST['submit']) && $_POST['submit'] == 'Add ')
  {
     if (checkInput() == false)
         renderForm("ERROR: Please fill in all required fields!",'Add', $companyList, '', '', '', '' );
     else
     {
	//SAVE NEW RECORD
        if($client->add() == '1')
	 $message = "1 record added.";
	else
	 $message = "ERROR Client not added.";
	setcookie("message", $message, time() + 3);
	header('location:client.php');
	exit();
     }
  }
  //Check if the form has been submitted for UPDATE
  else if (isset($_POST['submit']) && $_POST['submit'] == 'Update ')
  {
      if (checkInput() == false)
      {
      	 $client->retrieveByID($_GET['ID']);    
         renderForm("ERROR: Please fill in all required fields!", 'Update', $client->fname, $client->lname, $companyList, $client->company, $client->contactNo );
      }
      else
      {
        //UPDATE CURRENT RECORD
        
        if($client->update($_GET['ID']) == '1')
	 $message = "Record updated.";
	else
	 $message = "ERROR Unable to complete operation.";;
	setcookie("message", $message, time() + 3);
	header('location:clientList.php');
	exit();
      }
  }
  
  //FUNCTIONS
  function CheckInput()
  {
     if ($_POST['last'] == '')
     	return false;
     return true;
  }
  function displayMessage()
  {
     if (isset($error) && $error != '')
      echo '<div id="notification">'.$error.'</div>';
     if(isset($_COOKIE['message']))
       echo '<div id="notification">'.$_COOKIE["message"].'</div>';
  }
?>
